package com.fm.mediasoup.sdpTransform;

import com.fm.mediasoup.sdp.Fingerprint;
import com.fm.mediasoup.sdp.Groups;
import com.fm.mediasoup.sdp.Media;
import com.fm.mediasoup.sdp.MsidSemantic;
import com.fm.mediasoup.sdp.Origin;
import com.fm.mediasoup.sdp.Timing;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Sdp {
    public String extmapAllowMixed;
    public Groups[] groups;
    public Media[] media;
    public MsidSemantic msidSemantic;
    public String name;
    public String icelite;
    public Fingerprint fingerprint;
    public Origin origin;
    public Timing timing;
    public Integer version;

    public Sdp(){}

    public Sdp(Builder builder) {
        extmapAllowMixed = builder.extmapAllowMixed;
        groups = builder.groups;
        media = builder.media;
        msidSemantic = builder.msidSemantic;;
        name = builder.name;
        icelite = builder.icelite;
        fingerprint = builder.fingerprint;
        origin = builder.origin;
        timing = builder.timing;
        version = builder.version;
    }

    public List<Fingerprint> getFingerprintList(){
        List<Fingerprint> fingerprintList = new ArrayList<>();
        for (Media media : this.media) {
            fingerprintList.add(media.fingerprint);
        }
        return fingerprintList;
    }

    public static class Builder{
        public Builder setExtmapAllowMixed(String extmapAllowMixed) {
            this.extmapAllowMixed = extmapAllowMixed;
            return this;
        }


        public Builder setGroups() {
            List<Groups> groupsList = new ArrayList<>();
            Groups groups = new Groups();
            groups.mids = "";
            for (Media media: this.media){
                groups.mids += media.mid + " ";
            }
            groups.mids = groups.mids.substring(0, groups.mids.length() - 1);

            groups.type = "BUNDLE";
            groupsList.add(groups);
            this.groups = new Groups[groupsList.size()];
            groupsList.toArray(this.groups);
            return this;
        }

        public Builder setMedia(Media[] media) {
            this.media = media;
            return this;
        }

        public Builder setMsidSemantic(MsidSemantic msidSemantic) {
            this.msidSemantic = msidSemantic;
            return this;
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setIcelite(String icelite) {
            this.icelite = icelite;
            return this;
        }

        public Builder setFingerprint(Fingerprint fingerprint) {
            this.fingerprint = fingerprint;
            return this;
        }

        public Builder setOrigin(Origin origin) {
            this.origin = origin;
            return this;
        }

        public Builder setTiming(Timing timing) {
            this.timing = timing;
            return this;
        }

        public Builder setVersion(Integer version) {
            this.version = version;
            return this;
        }

        private String extmapAllowMixed;
        private Groups[] groups;
        private Media[] media;
        private MsidSemantic msidSemantic;
        private String name;
        private String icelite;
        private Fingerprint fingerprint;
        private Origin origin;
        private Timing timing;
        private Integer version;
        public Sdp build(){
            return new Sdp(this);
        }
    }

    @Override
    public String toString() {
        return "Sdp{" +
                "extmapAllowMixed='" + extmapAllowMixed + '\'' +
                ", groups=" + Arrays.toString(groups) +
                ", media=" + Arrays.toString(media) +
                '}';
    }
}
